﻿using System.Diagnostics;
using WPFServices.Models;

namespace WPFServices.Services
{
    public class ProcessService
    {
        private readonly ApiClient _apiClient;

        public ProcessService()
        {
            _apiClient = new ApiClient();
        }

        /// <summary>
        /// 获取所有流程
        /// </summary>
        public async Task<List<Process>> GetProcessesAsync()
        {
            var result = await _apiClient.GetAsync<List<Process>>("Process/GetProcess");

            if (result.IsSuc && result.code == ResultCode.Ok)
            {
                return result.data ?? new List<Process>();
            }
            else
            {
                throw new Exception($"获取流程列表失败: {result.msg}");
            }
        }

        /// <summary>
        /// 根据ID获取流程
        /// </summary>
        public async Task<Process> GetProcessByIdAsync(int id)
        {
            var result = await _apiClient.GetAsync<Process>($"Process/GetProcess/{id}");

            if (result.IsSuc && result.code == ResultCode.Ok)
            {
                return result.data;
            }
            else
            {
                throw new Exception($"获取流程详情失败: {result.msg}");
            }
        }

        /// <summary>
        /// 创建流程
        /// </summary>
        public async Task<Process> CreateProcessAsync(Process process)
        {
            var result = await _apiClient.PostAsync<Process>("Process/CreateProcess", process);

            if (result.IsSuc && result.code == ResultCode.Ok)
            {
                return result.data;
            }
            else
            {
                throw new Exception($"创建流程失败: {result.msg}");
            }
        }

        /// <summary>
        /// 更新流程
        /// </summary>
        public async Task<Process> UpdateProcessAsync(int id, Process process)
        {
            var result = await _apiClient.PutAsync<Process>($"Process/UpdateProcess/{id}", process);

            if (result.IsSuc && result.code == ResultCode.Ok)
            {
                return result.data;
            }
            else
            {
                throw new Exception($"更新流程失败: {result.msg}");
            }
        }

        /// <summary>
        /// 删除流程
        /// </summary>
        public async Task<bool> DeleteProcessAsync(int id)
        {
            var result = await _apiClient.DeleteAsync($"Process/DeleteProcess/{id}");

            if (result.IsSuc && result.code == ResultCode.Ok)
            {
                return true;
            }
            else
            {
                throw new Exception($"删除流程失败: {result.msg}");
            }
        }

        /// <summary>
        /// 获取分页流程数据
        /// </summary>
        public async Task<ApiPaging<List<Process>>> GetProcessesPagedAsync(int pageIndex, int pageSize)
        {
            var result = await _apiClient.GetAsync<ApiPaging<List<Process>>>($"Process/GetProcessPaged?pageIndex={pageIndex}&pageSize={pageSize}");

            if (result.IsSuc && result.code == ResultCode.Ok)
            {
                return result.data;
            }
            else
            {
                throw new Exception($"获取分页流程数据失败: {result.msg}");
            }
        }
    }
}